package ink.xiaobaibai.controller;

import com.alibaba.fastjson.JSON;
import ink.xiaobaibai.autoConfig.roleResources.ResourcesRoleMapping;
import ink.xiaobaibai.common.ImageInfo;
import ink.xiaobaibai.common.MapInfo;
import ink.xiaobaibai.common.MyRole;
import ink.xiaobaibai.common.SettingCommon;
import ink.xiaobaibai.response.ResponseFormat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

/**
 * @description: 基础设置接口
 * @author: 小白白
 * @create: 2021-07-17
 **/

@RestController
@RequestMapping("/setting")
@Api(tags = "基本信息接口")
@ResourcesRoleMapping(resourceName = "基本设置接口", includeRoleNames = MyRole.ADMIN)
@CrossOrigin
public class SettingController {

    /**
     * 押金金额
     */
    @GetMapping("/get-deposit")
    @ApiOperation("获取押金金额")
    public ResponseFormat getDepositKey() {
        return ResponseFormat.success(SettingCommon.getDepositKey());
    }

    @PutMapping("/update-deposit")
    @ApiOperation("修改押金金额")
    public ResponseFormat updateDepositKey(@RequestBody String amountStr) {
        BigDecimal amount;
        try {
            amount = new BigDecimal(amountStr);
        } catch (Exception e) {
            return ResponseFormat.fail("请输入正确的金额");
        }
        if (amount.doubleValue() <= 0) {
            return ResponseFormat.fail("押金不可小于等于0元");
        }
        SettingCommon.setValue(SettingCommon.DEPOSIT_KEY, amount.toString());
        return ResponseFormat.success();
    }

    /**
     * 月费金额
     */
    @GetMapping("/get-monthlyFee")
    @ApiOperation("获取月费金额")
    public ResponseFormat getMonthlyFee() {
        return ResponseFormat.success(SettingCommon.getMonthlyFee());
    }

    @PutMapping("/update-monthlyFee")
    @ApiOperation("修改月费金额")
    public ResponseFormat updateMonthlyFee(@RequestBody String amountStr) {
        BigDecimal amount;
        try {
            amount = new BigDecimal(amountStr);
        } catch (Exception e) {
            return ResponseFormat.fail("请输入正确的金额");
        }
        if (amount.doubleValue() <= 0) {
            return ResponseFormat.fail("月费不可小于等于0元");
        }
        SettingCommon.setValue(SettingCommon.MONTHLY_FEE_KEY, amount.toString());
        return ResponseFormat.success();
    }

    /**
     * 轮播图
     */
    @GetMapping("/get-CarouselImage")
    @ApiOperation("获取轮播图")
    public ResponseFormat getCarouselImage() {
        return ResponseFormat.success(SettingCommon.getCarouselImage());
    }

    @PutMapping("/update-CarouselImage")
    @ApiOperation("修改轮播图")
    public ResponseFormat updateCarouselImage(@RequestBody String carouselImage) {
        if (!this.strLengthValid(carouselImage)) {
            return ResponseFormat.fail("图片太多了");
        }
        SettingCommon.setValue(SettingCommon.CAROUSEL_IMAGE, carouselImage);
        return ResponseFormat.success();
    }

    /**
     * 使用协议
     */
    @GetMapping("/get-useAgreement")
    @ApiOperation("获取使用协议")
    public ResponseFormat getUseAgreement() {
        return ResponseFormat.success(SettingCommon.getUseAgreement());
    }

    @PutMapping("/update-useAgreement")
    @ApiOperation("修改使用协议")
    public ResponseFormat updateUseAgreement(@RequestBody String useAgreement) {
        if (!this.strLengthValid(useAgreement)) {
            return ResponseFormat.fail("字数限制1~4080");
        }
        SettingCommon.setValue(SettingCommon.USE_AGREEMENT, useAgreement);
        return ResponseFormat.success();
    }

    /**
     * 获取地图信息
     */
    @GetMapping("/get-map")
    @ApiOperation("获取地图信息 | [点亮城市,全国范围]")
    public ResponseFormat getMapInfo() {
        return ResponseFormat.success(SettingCommon.getMapInfo());
    }

    @PutMapping("/update-map")
    @ApiOperation("修改地图信息 | [点亮城市,全国范围]")
    public ResponseFormat updateMapInfo(@RequestBody @Validated MapInfo mapInfo) {
        SettingCommon.setValue(SettingCommon.MAP_KEY, JSON.toJSONString(mapInfo));
        return ResponseFormat.success();
    }

    /**
     * 单次业务使用协议
     */
    @GetMapping("/get-onePay-agreement")
    @ApiOperation("获取单次业务使用协议")
    public ResponseFormat getOnePay() {
        return ResponseFormat.success(SettingCommon.getOnepayAgreement());
    }

    @PutMapping("/update-onePay-agreement")
    @ApiOperation("修改单次业务使用协议")
    public ResponseFormat updateOnePay(@RequestBody String onePay) {
        if (!this.strLengthValid(onePay)) {
            return ResponseFormat.fail("字数限制1~4080");
        }
        SettingCommon.setValue(SettingCommon.ONEPAY_AGREEMENT, onePay);
        return ResponseFormat.success();
    }

    /**
     * 押金协议
     */
    @GetMapping("/get-deposit-agreement")
    @ApiOperation("获取押金缴纳协议")
    public ResponseFormat getDepositAgreement() {
        return ResponseFormat.success(SettingCommon.getDepositAgeement());
    }

    @PutMapping("/update-deposit-agreement")
    @ApiOperation("修改押金缴纳协议")
    public ResponseFormat updateDepositAgreement(@RequestBody String deposit) {
        if (!this.strLengthValid(deposit)) {
            return ResponseFormat.fail("字数限制1~4080");
        }
        SettingCommon.setValue(SettingCommon.DEPOSIT_AGEEMENT, deposit);
        return ResponseFormat.success();
    }

    /**
     * 月费协议
     */
    @GetMapping("/get-mon-agreement")
    @ApiOperation("获取月费业务协议")
    public ResponseFormat getMonAgreement() {
        return ResponseFormat.success(SettingCommon.getMonthlyAggement());
    }

    @PutMapping("/update-mon-agreement")
    @ApiOperation("修改月费业务协议")
    public ResponseFormat updateMonAgreement(@RequestBody String mon) {
        if (!this.strLengthValid(mon)) {
            return ResponseFormat.fail("字数限制1~4080");
        }
        SettingCommon.setValue(SettingCommon.MONTHLY_AGGEMENT, mon);
        return ResponseFormat.success();
    }

    /**
     * 购买协议
     */
    @GetMapping("/get-buy-agreement")
    @ApiOperation("获取购买业务协议")
    public ResponseFormat getBuyAgreement() {
        return ResponseFormat.success(SettingCommon.getBuyAggement());
    }

    @PutMapping("/update-buy-agreement")
    @ApiOperation("修改购买业务协议")
    public ResponseFormat updateBuyAgreement(@RequestBody String buy) {
        if (!this.strLengthValid(buy)) {
            return ResponseFormat.success("字数限制1~4080");
        }
        SettingCommon.setValue(SettingCommon.BUY_AGGEMENT, buy);
        return ResponseFormat.success();
    }

    /**
     * 主界面四张图文
     */
    @GetMapping("/get-main-info")
    @ApiOperation("获取主界面的四张图文(四个业务的图文)顺序[定点,月费,购买,酒店]")
    public ResponseFormat getMainInfo() {
        List<ImageInfo> mainImageInfo = SettingCommon.getMainImageInfo();
        return ResponseFormat.success(mainImageInfo);
    }

    @PutMapping("/update-main-info")
    @ApiOperation("修改主界面的四张图文(四个业务的图文)")
    public ResponseFormat updateMainInfo(@RequestBody @Valid List<ImageInfo> list) {
        String str = JSON.toJSONString(list);
        if (!this.strLengthValid(str)) {
            return ResponseFormat.fail("字数太长了");
        }
        SettingCommon.setValue(SettingCommon.MAIN_IMAGE_INFO, str);
        return ResponseFormat.success();
    }

    /**
     * 托管协议
     */
    @GetMapping("/get-escrow-agreement")
    @ApiOperation("获取托管协议")
    public ResponseFormat getEscrowAgreement() {
        String escrowAgreement = SettingCommon.getEscrowAgreement();
        return ResponseFormat.success(escrowAgreement);
    }

    @PutMapping("/update-escrow-agreement")
    @ApiOperation("修改托管协议")
    public ResponseFormat updateEscrowAgreement(@RequestBody String escrow) {
        if (!this.strLengthValid(escrow)) {
            return ResponseFormat.success("字数限制1~4080");
        }
        SettingCommon.setValue(SettingCommon.ESCROW_AGREEMENT, escrow);
        return ResponseFormat.success();
    }

    /**
     * 关于我们
     */
    @GetMapping("/get-about")
    @ApiOperation("关于我们")
    public ResponseFormat getAbout() {
        String about = SettingCommon.getAbout();
        return ResponseFormat.success(about);
    }

    @PutMapping("/update-about")
    @ApiOperation("修改关于我们")
    public ResponseFormat updateAbout(@RequestBody String about) {
        if (!this.strLengthValid(about)) {
            return ResponseFormat.success("字数限制1~4080");
        }
        SettingCommon.setValue(SettingCommon.ABOUT_KEY, about);
        return ResponseFormat.success();
    }

    private boolean strLengthValid(String str) {
        return StringUtils.isNotBlank(str) && str.length() < 4080;
    }

}
